package com.basketball.gym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basketball.gym.mapper.UserMapper;
import com.basketball.gym.pojo.Feedback;
import com.basketball.gym.mapper.FeedbackMapper;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.User;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.FeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵肖云
 * @since 2021-04-10
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 提交反馈
     */
    @Override
    public ResponseData<Feedback> postFeedback(Feedback feedback, User user) {
        feedback.setUserId(user.getId());
        feedbackMapper.insert(feedback);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),feedback);
    }

    /**
     * 查询我的反馈
     */
    @Override
    public ResponseData<List<Feedback>> getMyFeedback(User user) {
        QueryWrapper<Feedback> feedbackQueryWrapper=new QueryWrapper<>();
        feedbackQueryWrapper.eq("user_id",user.getId());
        feedbackQueryWrapper.orderByDesc("create_at");
        List<Feedback> feedbackList=feedbackMapper.selectList(feedbackQueryWrapper);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),feedbackList);
    }

    /**
     * 查询所有反馈
     */
    @Override
    public ResponseData<IPage<Feedback>> adminGetFeedback(Feedback feedback) {
        QueryWrapper<Feedback> feedbackQueryWrapper=new QueryWrapper<>();
        if(feedback.getUserId()!=null){
            feedbackQueryWrapper.eq("user_id",feedback.getUserId());
        }
        feedbackQueryWrapper.orderByDesc("create_at");
        Page<Feedback> feedbackPage=new Page<>();
        if (feedback.getCurrent() != null && feedback.getSize() != null) {
            feedbackPage = new Page<>(feedback.getCurrent(), feedback.getSize());
        }
        IPage<Feedback> feedbackIPage=feedbackMapper.selectPage(feedbackPage,feedbackQueryWrapper);
        for(Feedback feedbackNode:feedbackIPage.getRecords()){
            feedbackNode.setUser(userMapper.selectById(feedbackNode.getUserId()));
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),feedbackIPage);
    }
}
